package de.negativrekord.blockworld.geometry;

import cc.creativecomputing.graphics.CCDrawMode;
import cc.creativecomputing.graphics.CCGraphics;
import cc.creativecomputing.math.CCMath;
import cc.creativecomputing.math.CCVector2f;
import cc.creativecomputing.math.CCVector3f;
import cc.creativecomputing.math.spline.CCBezierSpline;
import cc.creativecomputing.math.spline.CCSpline;
import cc.creativecomputing.math.spline.CCSpline.CCSplineType;

public class Sector {
	int[] neighbors = new int[4];
	CCVector3f normal;
	CCVector3f center;
	CCVector2f offset;
	int size;
	float timePassed = 0f;
	float maxTime = 10f;
	
	public Sector (CCVector3f theCenter, CCVector3f theNormal, CCVector2f theOffset, int theSize) {
		offset = theOffset;
		normal = theNormal;
		center = theCenter;
		size = theSize;
		
	}
	
//	public CCVector2f wrapMove (CCVector2f p) {
//		if (p.y>size) {
//			p.subtract(new CCVector2f(0,size));
//			currentSector = sectors[currentSector].neighbors[0];
//		}
//		if (p.x>size) {
//			p.subtract(new CCVector2f(size,0));
//			currentSector = sectors[currentSector].neighbors[1];
//			
//		}
//		if (p.y<0) {
//			p.add(new CCVector2f(0,size));
//			currentSector = sectors[currentSector].neighbors[2];
//		}
//		if (p.x<0) {
//			p.add(new CCVector2f(size,0));
//			currentSector = sectors[currentSector].neighbors[3];
//		}
//		return p;
//	}
	
	
	// reflect
	public void wrapMove (CCVector2f p, CCVector2f dir) {
		CCVector2f n = new CCVector2f(0,0);
		if (p.y>size/2) {
			n = new CCVector2f (0,-1);
		}
		if (p.x>size/2) {
			n = new CCVector2f (-1,0);
		}
		if (p.y<-size/2) {
			n = new CCVector2f (0,1);
		}
		if (p.x<-size/2) {
			n = new CCVector2f (1,0);
		}
		dir.subtract (new CCVector2f(n).scale(2*dir.dot(n)));
	}
	
	public void setNeighbors (int n0, int n1, int n2, int n3) {
		neighbors[0] = n0;
		neighbors[1] = n1;
		neighbors[2] = n2;
		neighbors[3] = n3;
	}
	
	public void drawFlat (CCGraphics g) {
		g.rect(offset.x,offset.y,size,size);
	}
	
	public void draw3D (CCGraphics g) {
		
		g.pushMatrix();
		g.translate(new CCVector3f(center));
		g.rotateY(normal.x*90);
		g.rotateX(normal.y*90);
		
		CCVector3f v0 = new CCVector3f(0,0,0);
		CCVector3f v1 = new CCVector3f(size,0,0);
		CCVector3f v2 = new CCVector3f(size,size,0);
		CCVector3f v3 = new CCVector3f(0,size,0);
		g.beginShape(CCDrawMode.QUADS);
		g.vertex (v0);
		g.vertex (v1);
		g.vertex (v2);
		g.vertex (v3);
		g.endShape();
		
		g.popMatrix();
	}
	
//	public void drawPoint (CCGraphics) {
//		g.pushMatrix();
//		CCVector3f p = new CCVector3f(normal);
//		p.scale(-size/2);
//		
//		if (normal.x!=0) {
//			p.y += point.y-size/2;
//			p.z+=(size/2-point.x)*normal.x;
//		}
//		if (normal.y!=0) {
//			p.x+=size/2-point.x;
//			p.z+=(size/2-point.y)*normal.y;
//		}
//		if (normal.z!=0) {
//			p.x+=(point.x-size/2)*normal.z;
//			p.y+=(point.y-size/2)*normal.z;
//		}
//		
//		g.point(p);
//		g.popMatrix();
//	}
}